package com.shanghaionstar.user.presenter;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.response.HeadImgRes;
import com.onstar.cn.more.response.PersonalInfoResponse;
import com.shanghaionstar.activity.MoreActivity;
import com.shanghaionstar.user.iview.IAccountInfoView;
import com.shanghaionstar.user.service.AccountInfoService;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by 7828wc on 16/7/1.
 */
public class AccountInfoPresenter extends BasePresenter<Object> {
    private IAccountInfoView accountView;
    private LogManager log = LogManager.getInstance();
    private AccountInfoService accountInfoService;

    public AccountInfoPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<Object> view) {
        super.setView(view);
        this.accountView = (IAccountInfoView) view;
        accountInfoService = new AccountInfoService(accountView.onActivity());
        super.setServiceProvider(accountInfoService);
    }

    public void updateUserInfo(final String licenseExpireDate) {
        accountView.setLoading(true);
        subscription = accountInfoService.init().flatMap(new Func1<String, Observable<PersonalInfoResponse>>() {
            @Override
            public Observable<PersonalInfoResponse> call(String entity) {
                return accountInfoService.updateUserInfo(licenseExpireDate);
            }
        }).subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread()).subscribe(new Action1<PersonalInfoResponse>() {
                    @Override
                    public void call(PersonalInfoResponse geofenceList) {
                        accountView.setLoading(false);
                        accountView.onReponseSuccess(geofenceList);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //抛出Error异常
//                        accountView.onResponseError((OnStarException) throwable);
                        accountView.onUpdateInfoFailed(throwable);
                        accountView.setLoading(false);
                    }
                });
    }

    public void getData(){
      //  accountView.setLoading(true);
        subscription=accountInfoService.getObserval().subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<HeadImgRes>() {
                    @Override
                    public void call(HeadImgRes headImgRes) {
                        if(!CommonUtil.isNull(headImgRes)){

                            accountView.onReponseSuccess(headImgRes);
                       //     accountView.setLoading(false);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        accountView.onResponseError((OnStarException) throwable);
                  //      accountView.setLoading(false);
                    }
                });
    }


    public void showActionSheet() {
        int showActionsheet = MoreActivity.SHOW_ACTIONSHEET;
        ((MoreActivity)accountView.onActivity()).showDialogToTakeChoosePicture(showActionsheet);
    }


    public void showPortrait(HeadImgRes headImgRes){
        accountView.showPortrait(headImgRes);
    }
}
